$(document).ready(function(){
    prettyPrint();
    
	$('#login-box-hidden').center();
	$('a#login').click(function(){
		showShadow();
		$('#login-box-hidden').center();
		$('#login-box-hidden').fadeIn();
		return false;
	});
	

    $('#login').click(function(e){
        var position = $(this).position();
        $('#login-box').css("left", (position.left - 200) + "px");
        $('#login-box').css("top", (position.top + 18) + "px");
        $('#login-box').toggle();
    });
    // Chiusura messaggi (error, success,...)
    $('.border .internal a.close').click(function(){
        $(this).parent().parent().parent().slideUp();
    });
    
    var mouse_inside_categories;
    $('#main-categories, div#search a, div#search span').hover(function(){ 
    	mouse_inside_categories=true; 
    }, function(){ 
    	mouse_inside_categories=false; 
    });
    $("body").mouseup(function(){ 
        if(! mouse_inside_categories) $('#main-categories').hide();
    });
    $('#main-categories ul li a').click(function(){
    	var idCat = $(this).attr("idCat");
    	$('#search form #id-category').val(idCat);
    	$('#search form span a').html($(this).text());
    	$('#main-categories').hide();
    	if($('#search .arrow-down').hasClass('arrow-up')){
    		$('#search .arrow-down').removeClass('arrow-up');
    	}
    	ajaxSearch();
    });
});
function showShadow(){
	$('#shadow').show();
}
function startLoading(){
    $('#loader').fadeIn();
}
function stopLoading(){
    $('#loader').fadeOut();
}
function loadingError(text){
	stopLoading();
	$('#loader-error').html(text);
	$('#loader-error').slideDown();
	setTimeout( function() {
		$('#loader-error').slideUp();
	}, 3000 );
	
}
/* Center the element on the middle of the page */
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
jQuery.fn.centerHome = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop()-150 + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
function closeLogin(){
	$('#login-box-hidden').fadeOut();
	$('#shadow').hide();
}
function selectMainCategory(id){
	$('#main-categories ul li a').click();
}
function microtime () {
    var now = new Date().getTime() / 1000;
    return now;
}